<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="admin_header.jsp"/>
<!-- 主内容区 -->
<main role="main" class="col-md-10 ml-sm-auto px-4">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">编辑新闻</h1>
    </div>

    <form action="newsEdit" method="post" enctype="multipart/form-data">
        <input type="hidden" name="new_id" value="${news.new_id}">

        <div class="mb-3">
            <label for="title" class="form-label">新闻标题</label>
            <input type="text" class="form-control" id="title" name="title"
                   value="${news.title}" required>
        </div>

        <div class="mb-3">
            <label for="sort_id" class="form-label">新闻分类</label>
            <select class="form-control" id="sort_id" name="sort_id" required>
                <c:forEach items="${sortList}" var="sort">
                    <option value="${sort.sort_id}"
                        ${news.sort_id == sort.sort_id ? 'selected' : ''}>
                            ${sort.name}
                    </option>
                </c:forEach>
            </select>
        </div>

        <div class="mb-3">
            <label for="content" class="form-label">新闻内容</label>
            <textarea class="form-control" id="content" name="content"
                      rows="10" required>${news.content}</textarea>
        </div>

        <div class="mb-3">
            <label class="form-label">当前图片</label>
            <c:if test="${not empty news.img_url}">
                <div class="mb-2">
                    <img src="../images/${news.img_url}" alt="当前图片"
                         style="max-width: 200px;" class="img-thumbnail">
                </div>
            </c:if>
            <div class="form-check mb-2">
                <input class="form-check-input" type="checkbox" id="removeImage"
                       name="removeImage" value="true">
                <label class="form-check-label" for="removeImage">
                    删除当前图片
                </label>
            </div>
        </div>

        <div class="mb-3">
            <label for="image" class="form-label">更换图片</label>
            <input type="file" class="form-control" id="image" name="image"
                   accept="image/*">
        </div>

        <button type="submit" class="btn btn-danger">保存修改</button>
        <a href="newsManage" class="btn btn-secondary">返回</a>
    </form>
</main>
<script>
    // 处理删除图片复选框和文件上传的联动
    $('#image').on('change', function () {
        if (this.files.length > 0) {
            $('#removeImage').prop('checked', false);
            $('#removeImage').prop('disabled', true);
        } else {
            $('#removeImage').prop('disabled', false);
        }
    });
</script>

</body>
</html> 